package ihm.start;

import ihm.GUIManager;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

import model.Classe;
import model.VariableStatiques;
import corpus.ActionsTexteFichier;
import corpus.CreateInstance;

public class StartPanel extends JPanel implements ActionListener {


	private static final long serialVersionUID = -2526412164310040381L;

	private GridBagConstraints c;

	private JButton openFileButton;
	//private JButton generateInstances;
	private JLabel fileName;

	private File file;
	private DirectoryChooser dc;



	public StartPanel(){

		setLayout(new GridBagLayout());
		c = new GridBagConstraints();

		createFileChooser();
		createFileTextBox();
	}


	public void actionPerformed(ActionEvent e) {

		if(e.getSource() == openFileButton){
			int valid = dc.showOpenDialog(StartPanel.this);
			if (valid == JFileChooser.APPROVE_OPTION){
				file = dc.getSelectedFile();
				VariableStatiques.LIST_CLASSES = new ArrayList<Classe>();
				ActionsTexteFichier.parcoursFichiers(file, VariableStatiques.LIST_CLASSES);
				//generateInstances.setEnabled(true);

			}
			VariableStatiques.FILTER_INSTANCES = CreateInstance.CreateInstance(VariableStatiques.LIST_CLASSES);
			VariableStatiques.STRING_TO_VECTOR.setInputFormat();
			VariableStatiques.STRING_TO_VECTOR_MATRICE.setInputFormat();
			try {
				VariableStatiques.MAIN_INSTANCES = VariableStatiques.STRING_TO_VECTOR.outputFilter(VariableStatiques.FILTER_INSTANCES);
				//generateInstances.setEnabled(false);
				GUIManager.getInstance().getMainTabbedPane().SetTabEnabled();
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}



	}


	private void createFileChooser(){
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.CENTER;
		c.gridwidth = 3;
		c.insets = new Insets(10, 10, 5, 10);
		c.gridx = 0;
		c.gridy = 0;


		dc = new DirectoryChooser();
		openFileButton  = new JButton("Sélection DOSSIER");
		openFileButton.addActionListener(this);
		add(openFileButton, c );

	}

	/**
	 * 
	 */
	private void createFileTextBox(){
		fileName = new JLabel();
		c.fill = GridBagConstraints.HORIZONTAL;
		c.anchor = GridBagConstraints.CENTER;
		c.gridwidth = 3;
		c.gridx = 0;
		c.gridy = 1;
		c.insets = new Insets(5, 6, 5, 6);
		add(fileName, c);

	}


	private class DirectoryChooser extends JFileChooser{

		public DirectoryChooser(){
			setDialogTitle("Sélection DOSSIER");
			setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		}

	}



}
